part of 'api.dart';

extension ApiCommunity on _Api {
  ///获取社区分类

  Future<List<CommunityClassifyModel>> getCommunityClassificationLists({
    bool isAll = false,
  }) async {
    try {
      final response = await httpInstance.get(
        url: 'coterie/list',
        queryMap: {
          'isAll': isAll,
        },
        complete: CommunityClassifyModel.fromJson,
      );
      return response ?? [];
    } catch (e) {
      return [];
    }
  }

  ///获取社区列表
  ///[type] 1-精选 2-最新 3-最热
  Future<List<CommunityModel>> getCommunityDynamicLists({
    required String id,
    required int page,
    int pageSize = 20,
    int type = 1,
  }) async {
    Map<String, dynamic> request = {
      'page': page,
      'pageSize': pageSize,
      'type': type
    };
    if (id.isNotEmpty) {
      request['id'] = id;
    }
    try {
      final response = await httpInstance.get(
        url: 'community/dynamic/list',
        queryMap: request,
        complete: CommunityModel.fromJson,
      );
      return response ?? [];
    } catch (e) {
      return [];
    }
  }

  ///获取动态详情
  Future<CommunityModel?> getCommunityDynamicDetails({
    required int dynamicId,
  }) async {
    try {
      final response = await httpInstance.get(
        url: 'community/dynamic/dynamicInfo',
        queryMap: {'dynamicId': dynamicId},
        complete: CommunityModel.fromJson,
      );
      return response;
    } catch (e) {
      return null;
    }
  }

  ///点赞或者取消点赞
  likeOrCancelLike({required bool isLike, required int dynamicId}) async {
    try {
      await httpInstance.post(
        url: isLike ? 'community/dynamic/like' : 'community/dynamic/unLike',
        body: {'dynamicId': dynamicId},
      );
      return true;
    } catch (e) {
      return false;
    }
  }

  // 评论点赞
  Future<bool?> dynamicCommentLike(int commentId) async {
    try {
      await httpInstance.post(url: 'community/dynamic/comment/saveLike', body: {
        'commentId': commentId,
      });
      return true;
    } catch (e) {
      return null;
    }
  }

  // 取消评论点赞
  Future<bool?> dynamicCommentUnLike(int commentId) async {
    try {
      await httpInstance.post(
        url: 'community/dynamic/comment/unLike',
        body: {'commentId': commentId},
      );
      return true;
    } catch (e) {
      return null;
    }
  }

  // toogle 点赞
  Future<bool?> toogleDynamicCommentLike(int commentId, {required bool? like}) {
    if (like == true) {
      return dynamicCommentUnLike(commentId);
    } else {
      return dynamicCommentLike(commentId);
    }
  }

  ///获取社区列表
  ///[type] 1-精选 2-最新 3-最热
  Future<List<CommunityModel>> getUserDynamicLists({
    required int userId,
    required int page,
    int pageSize = 20,
    int? dynamicType,
  }) async {
    Map<String, dynamic> request = {
      'page': page,
      'pageSize': pageSize,
      'userId': userId
    };
    if (dynamicType != null && dynamicType >= 0) {
      request['dynamicType'] = dynamicType;
    }
    try {
      final response = await httpInstance.get(
        url: 'community/dynamic/person/list',
        queryMap: request,
        complete: CommunityModel.fromJson,
      );
      return response ?? [];
    } catch (e) {
      return [];
    }
  }



  ///获取圈子列表
  Future<dynamic> getPublishTags() async {
    try {
      final response = await httpInstance.get(
        url: '/coterie/list',
        queryMap: {'isAll': true},
      );
      return response;
    } catch (e) {
      return null;
    }
  }


  ///发布帖子
  Future<dynamic> publishRelease(Map<String, dynamic>? body) async {
    try {
      await httpInstance.post(
        url: '/community/dynamic/release',
        body: body,
      );
      return true;
    } catch (e) {
      return false;
    }
  }
}
